package com.example.androidtraining_ahut2020;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivityDay11SharedPreference extends AppCompatActivity {
    private static final String FILENAME = "ahut"; // 保存的文件名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_day11_shared_preference);
        final SharedPreferences share = super.getSharedPreferences(FILENAME,
                Activity.MODE_PRIVATE);

        final SharedPreferences.Editor edit = share.edit();
        edit.putString("school", "ahut");
        edit.putInt("year", 2020);
        edit.commit();// 提交更新


        final Button readSchool = findViewById(R.id.button2);
        final TextView textView = findViewById(R.id.textView3);
        readSchool.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                textView.setText(share.getString("school", "未找到"));

            }
        });
    }
}